package com.tom;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.LogFactory;

import com.mysql.jdbc.log.Log;

/**
 * Servlet Filter implementation class LogFilter
 */
public class LogFilter implements Filter {

private	Log log = (Log) LogFactory.getLog(this.getClass());
private HttpServletRequest req;
private HttpServletRequest res;
			
	
    /**
     * Default constructor. 
     */
	
    public LogFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here

		// pass the request along the filter chain
		HttpServletRequest request1 =(HttpServletRequest) req;
		HttpServletRequest response1 = (HttpServletRequest) res;
		long startTime = System.currentTimeMillis();
		String requestURI= request1.getRequestURI();
		requestURI = request1.getQueryString()==null?requestURI : 
			(requestURI +"?" +request1.getQueryString());
		
		chain.doFilter(request1, (ServletResponse) response1);
	
	long endTime = System.currentTimeMillis();
	((org.apache.commons.logging.Log) log).info(request.getRemoteAddr()+"存取"+requestURI+",總用時"+(endTime-startTime)+"毫秒");

	}
	/**
	 * @see Filter#init(FilterConfig)
	 */
	/*public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}*/

	public void destroy() {
		// log.info("開始 : "+filterName);
		// TODO Auto-generated method stub
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
}
